package com.ruoyi.basicData.controller;

import java.io.InputStream;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.basicData.domain.SupplierMasterData;
import com.ruoyi.basicData.service.ISupplierMasterDataService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 供应商管理Controller
 * 
 * @author admin
 * @date 2024-06-27
 */
@RestController
@RequestMapping("/basicData/supplierMasterData")
public class SupplierMasterDataController extends BaseController
{
    @Autowired
    private ISupplierMasterDataService supplierMasterDataService;

    /**
     * 查询供应商管理列表
     */
    @PreAuthorize("@ss.hasPermi('basicData:supplierMasterData:list')")
    @GetMapping("/list")
    public TableDataInfo list(SupplierMasterData supplierMasterData)
    {
        startPage();
        List<SupplierMasterData> list = supplierMasterDataService.selectSupplierMasterDataList(supplierMasterData);
        return getDataTable(list);
    }

    /**
     * 导出供应商管理列表
     */
    @PreAuthorize("@ss.hasPermi('basicData:supplierMasterData:export')")
    @Log(title = "供应商管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SupplierMasterData supplierMasterData)
    {
        List<SupplierMasterData> list = supplierMasterDataService.selectSupplierMasterDataList(supplierMasterData);
        ExcelUtil<SupplierMasterData> util = new ExcelUtil<SupplierMasterData>(SupplierMasterData.class);
        util.exportExcel(response, list, "供应商管理数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<SupplierMasterData> util = new ExcelUtil<SupplierMasterData>(SupplierMasterData.class);
        util.importTemplateExcel(response, "供应商管理数据");
    }
    /**
     * 导入数据
     */
    @Log(title = "供应商管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('basicData:supplierMasterData:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<SupplierMasterData> util = new ExcelUtil<SupplierMasterData>(SupplierMasterData.class);
        InputStream inputStream = file.getInputStream();
        List<SupplierMasterData> list = util.importExcel(inputStream );
        inputStream.close();
        int count = supplierMasterDataService.batchInsertSupplierMasterData(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }


    /**
     * 获取供应商管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('basicData:supplierMasterData:query')")
    @GetMapping(value = "/{supplierId}")
    public AjaxResult getInfo(@PathVariable("supplierId") Long supplierId)
    {
        return success(supplierMasterDataService.selectSupplierMasterDataBySupplierId(supplierId));
    }

    /**
     * 新增供应商管理
     */
    @PreAuthorize("@ss.hasPermi('basicData:supplierMasterData:add')")
    @Log(title = "供应商管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SupplierMasterData supplierMasterData)
    {
        return toAjax(supplierMasterDataService.insertSupplierMasterData(supplierMasterData));
    }

    /**
     * 修改供应商管理
     */
    @PreAuthorize("@ss.hasPermi('basicData:supplierMasterData:edit')")
    @Log(title = "供应商管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SupplierMasterData supplierMasterData)
    {
        return toAjax(supplierMasterDataService.updateSupplierMasterData(supplierMasterData));
    }

    /**
     * 删除供应商管理
     */
    @PreAuthorize("@ss.hasPermi('basicData:supplierMasterData:remove')")
    @Log(title = "供应商管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{supplierIds}")
    public AjaxResult remove(@PathVariable Long[] supplierIds)
    {
        return toAjax(supplierMasterDataService.deleteSupplierMasterDataBySupplierIds(supplierIds));
    }
}
